var form;
var $;
var table;
var layer;
var upload;
var element;
var node;
var user = JSON.parse(localStorage.getItem("user"));
var userId = user.userId;

var data = [];
var microCircleData =  JSON.parse(localStorage.getItem("microCircleData"));
$("input[name=microcircleId]").val(microCircleData.microcircleId);
data.push(microCircleData);

console.log($("input[name=microcircleId]").val());

layui.use(['form','jquery','table','layer','upload'],function() {
    form = layui.form;
    $ = layui.jquery;
    table = layui.table;
    layer = layui.layer;
    upload = layui.upload;
    element = layui.element;

    //展示点赞等数量
    showNums(data);

    //展示微圈评论
    showMicroCircleComment(microCircleData.microcircleId);
});

function showNums(data) {
    table.render({
        elem:"#demo",
        data:data,
        width:500,
        cols:[[
            {field:'acticleHits',title:'查看量'},
            {field:'acticleLike',title:'点赞量'},
            {field:'acticleShare',title:'分享量'},
            {field:'articleHits',title:'收藏量',templet:function (data) {
                    var collectionNum = 0
                    $.ajax({
                        url:'/microcircle/getCollectionNum',
                        type: "post",
                        dataType:'json',
                        async: false,
                        data: {"microcircleId": data.microcircleId },
                        success:function (data) {
                            if (data.data != null){
                                collectionNum = data.data;
                            } else{
                                collectionNum = 0;
                            }
                        }
                    });
                    return collectionNum;
                }}
        ]]
    })
}

function showMicroCircleComment(microcircleId) {
    $.ajax({
        url: "/microcircle/getMicroCircleComment",
        type: "post",
        dataType: "json",
        data: {"commodityId": microcircleId},
        success: function (data) {
            console.log(data.data);
            var html = "";
            var data = data.data;
            if (data != ''){
                for (var i = 0; i < data.length; i++){
                    if (data[i].userType == "1"){
                        data[i].userName = data[i].nickName;
                        data[i].imgUrl = data[i].avatarUrl;
                    }
                    if (data[i].userType == "2"){
                        data[i].userName = data[i].trueName;
                        data[i].imgUrl = data[i].acatarUrl;
                    }
                    if (data[i].display == "2") {
                        data[i].yinStatu = "隐藏";
                    }
                    if (data[i].display == "3") {
                        data[i].yinStatu = "显示";
                    }
                    if (data[i].status == "3") {
                        data[i].delStatu = "已删除";
                    }
                    if (data[i].status == "1") {
                        data[i].delStatu = "删除";
                    }
                }
                for(var a = 0; a < data.length; a++){
                    if (data[a].status == "1") {
                        html+=
                            "<div class='contentdiv'>" +
                            "<div class='contentdiv1'>" +
                            "<img id='imgId2' src='"+ data[a].imgUrl +"'>" +
                            "</div>" +
                            "<div class='singlediv'>" +
                            "<p class='nameclass'>"+ data[a].userName +"</p>" +
                            "<p class='nameclass'>回复网友:"+ data[a].content +"</p>" +
                            "<p class='timeclass'>"+ data[a].commentDate+"</p>" +
                            "<div class='btnTwo'>" +
                            "<a class='a_class' href='javascript:;hideComment(\""+ data[a].id +"\",\""+ data[a].display+"\")'>"+ data[a].yinStatu +"</a>&nbsp" +
                            "<a class='a_class2' href='javascript:delectComment(\""+ data[a].id +"\")')'>"+ data[a].delStatu +"</a>" +
                            "</div>" +
                            "</div>" +
                            "</div>";
                    }else if (data[a].status == "3") {
                        html+=
                            "<div class='contentdiv'>" +
                            "<div class='contentdiv1'>" +
                            "<img id='imgId2' src='"+ data[a].imgUrl +"'>" +
                            "</div>" +
                            "<div class='singlediv'>" +
                            "<p class='nameclass'>"+ data[a].userName +"</p>" +
                            "<p class='nameclass'>回复网友:"+ data[a].content +"</p>" +
                            "<p class='timeclass'>"+ data[a].commentDate+"</p>" +
                            "<div class='btnTwo'>" +
                            "<a class='a_class2' href='javascript:delectComment(\""+ data[a].id +"\")')'>"+ data[a].delStatu +"</a>" +
                            "</div>" +
                            "</div>" +
                            "</div>";
                    }

                }
                $("#comment_id").html(html);
            }

            /*$(".a_class").on("click",function (e) {
                console.log($(e.target).text());
                if ($(e.target).text() == "隐藏"){
                    $(e.target).text("已隐藏");
                }else{
                    $(e.target).text("隐藏");
                }
            });*/

            /*$(".a_class2").on("click",function (e) {
                console.log($(e.target).text());
                if ($(e.target).text() == "删除"){
                    $(this).siblings("a").css("display","none");
                }
            });*/
            element.init();
            console.log(data);
        }
    });
}

//隐藏评论
function hideComment(id,display) {
    if (display == "2"){
        display = "3";
    } else if (display == "3"){
        display = "2";
    }
    console.log(id);
    console.log(display);
    $.ajax({
        url: "/microcircle/hideComment",
        type: "post",
        dataType: "json",
        data: {"id": id,"display": display},
        success: function (data) {
            console.log(data.data);
            if (data.data){
                //addLog(3,4,'隐藏微圈');
                showMicroCircleComment(microCircleData.microcircleId);
            }
        }
    });
}

//删除评论
function delectComment(id) {
    $.ajax({
        url: "/microcircle/delComment",
        type: "post",
        dataType: "json",
        data: {"id": id},
        success: function (data) {
            if (data.data){
                //addLog(3,4,'删除微圈');
                showMicroCircleComment(microCircleData.microcircleId);
            }
        }
    });
}

//添加操作日志
function addLog(logType,type,remark) {
    var user = JSON.parse(localStorage.getItem("user"));
    var userId = user.userId;
    $.ajax({
        url: "/microcircle/addLog",
        dataType: "json",
        type: "post",
        data: {"logType":logType,"type":type,"userId":userId,"remark":remark},
        success: function (data) {

        }
    });
}

//确定
function sub() {
    var commodityId = $("input[name=microcircleId]").val();
    var content = $("#text_div1").val();
    var data = {
        userId: userId,
        commodityId: commodityId,
        content: content,
        createId: userId,
        updateId: userId,
    }
    console.log(data);
    $.ajax({
        url: "/microcircle/addMicroCircleComment",
        type: "post",
        dataType: "json",
        data: data,
        success: function (data) {
            if (data.data){
                showMicroCircleComment(microCircleData.microcircleId);
                layer.msg("添加成功");
            }
        }
    });
}

//刷新
function refresh() {
    window.location.reload();
}